package com.xuelang.pipeline.domain.pipeline.service.imp;

import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.PipelineTemplateQuery;
import com.xuelang.pipeline.domain.pipeline.PipelineTemplate;
import com.xuelang.pipeline.domain.pipeline.repository.PipelineTemplateRepository;
import com.xuelang.pipeline.domain.pipeline.service.PipelineTemplateDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流水线模板领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class PipelineTemplateDomainServiceImpl implements PipelineTemplateDomainService {

    @Autowired
    private PipelineTemplateRepository pipelineTemplateRepository;

    @Override
    public PageRecord<PipelineTemplate> pageList(long currentPage, long pageSize, PipelineTemplateQuery pipelineTemplateQuery) {
        return pipelineTemplateRepository.pageList(currentPage, pageSize, pipelineTemplateQuery);
    }

    @Override
    public List<PipelineTemplate> list(PipelineTemplateQuery pipelineTemplateQuery) {
        return pipelineTemplateRepository.list(pipelineTemplateQuery);
    }

    @Override
    public List<PipelineTemplate> list(PipelineTemplate pipelineTemplate) {
        return pipelineTemplateRepository.list(pipelineTemplate);
    }


    @Override
    public Boolean save(PipelineTemplate pipelineTemplate) {
        if(existsName(pipelineTemplate.getUuid(), pipelineTemplate.getName())) {
            throw new BizException("模板名称已经存在");
        }
        return pipelineTemplateRepository.save(pipelineTemplate);
    }

    @Override
    public Boolean update(PipelineTemplate pipelineTemplate) {
        if(existsName(pipelineTemplate.getUuid(), pipelineTemplate.getName())) {
            throw new BizException("模板名称已经存在");
        }
        return pipelineTemplateRepository.update(pipelineTemplate);
    }

    @Override
    public Boolean delete(String pipelineTemplateUuid) {
        return pipelineTemplateRepository.delete(pipelineTemplateUuid);
    }

    @Override
    public PipelineTemplate query(String pipelineTemplateUuid) {
        return pipelineTemplateRepository.query(pipelineTemplateUuid);
    }

    @Override
    public Boolean existsName(String pipelineTemplateUuid, String pipelineTemplateName) {
        return pipelineTemplateRepository.existsName(pipelineTemplateUuid, pipelineTemplateName);
    }

}
